package com.ellecity06.cwhiskymall.ui.mine

import androidx.lifecycle.viewModelScope
import com.ellecity06.common.base.BaseMviViewModel
import com.ellecity06.common.ktx.postEvent
import com.ellecity06.cwhiskymall.bean.request.VersionInfoRequest
import com.ellecity06.cwhiskymall.http.request
import com.ellecity06.cwhiskymall.storage.AppStorage
import com.ellecity06.cwhiskymall.storage.UserStorage
import kotlinx.coroutines.launch

/**
 * @author ellecity06
 * @time 2024/10/22 14:49
 * @des
 */
class MineViewModel : BaseMviViewModel<MineState, MineIntent>() {
    override suspend fun collectIntent(it: MineIntent) {

        when (it) {
            is MineIntent.FetchUserInfo -> {
                fetchUserInfo()
                fetchStatistical()
            }

            is MineIntent.Logout -> {
                logout()
            }

            is MineIntent.FetchLastVersion -> {
                lastVersion()
            }

            MineIntent.UpdateUser -> fetchUser()
            MineIntent.FetchFirstId -> {
                fetchFirstId()
            }

            is MineIntent.GetCurrentToken -> {
                getCurrentToken(it.isClick, it.clickType)
            }
        }
    }

    private fun getCurrentToken(click: Boolean, clickType: Int) {

        request(api = { getCultureToken() }, showLoading = true, success = {
            if (it?.token != null && it.token.isNotEmpty()) {
                AppStorage.assetsToken = it.token
                mState.postEvent(MineState.GetCurrentTokenSuccess(click, clickType))
            }

        })
    }

    private fun fetchFirstId() {
        request(api = { getFirstUnlockState() }, showLoading = true, success = {
            mState.postEvent(MineState.FirstOrderId(it ?: 0))
        })
    }

    private fun lastVersion() {
        request(api = { lastVersion(VersionInfoRequest()) }, success = {})
    }

    private fun logout() {
        request(api = { logout() }, showLoading = true, success = {
            mState.postEvent(MineState.LogoutSuccess)

        })
    }

    private fun fetchUser() {
        request(api = { userInfo() }, success = {
            if (it != null) {
                UserStorage.user = it
                mState.postEvent(MineState.UserInfo(it))
            }

        }, error = {

        })
    }

    private fun fetchUserInfo() {
//        request(api = { userInfo() }, success = {
//            if (it!=null){
//                mState.postEvent(MineState.UserInfo(it))
//            }
//
//        })
//        request(api = { userAccountInfo() }, success = {
//            if (it!=null){
//                mState.postEvent(MineState.UserAccountInfo(it))
//            }
//        })
        request(api = { userCenter() }, {
            if (it != null) {
                mState.postEvent(MineState.UserCenterInfo(it))
            }
        }, error = {
            viewModelScope.launch {
                mState.postEvent(MineState.Error)
            }
        })
    }

    private fun fetchStatistical() {
        request(api = { statistical() }, {
            if (it != null) {
                mState.postEvent(MineState.UserStatisticalInfo(it))
            }
        }, error = {
            viewModelScope.launch {
                mState.postEvent(MineState.Error)
            }
        })
    }
}